In [1]:
# Auto-generated by leda
import leda
leda.set_interact_mode(leda.StaticIpywidgetsInteractMode())
In [2]:
# Auto-generated by leda
import os
from leda.vendor.static_ipywidgets.static_ipywidgets import interact as static_interact
static_interact.IMAGE_MANAGER = static_interact.InlineImageManager()
leda demo: matplotlib¶
In [3]:
import abc
import dataclasses
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from typing_extensions import override
import leda
In [4]:
leda.init("matplotlib")
plt.rcParams.update({"figure.max_open_warning": 0})
In [5]:
%%HTML
<!-- Auto-generated by leda -->
<h2>Table of Contents</h2>
<ol type='I'>
<li type='I'><a href='#leda-demo:-matplotlib'>leda demo: matplotlib</a></li>
<ol type='A'>
<li type='A'><a href='#Info'>Info</a></li>
<li type='A'><a href='#Data'>Data</a></li>
<li type='A'><a href='#Visualization'>Visualization</a></li>
<ol type='1'>
<li type='1'><a href='#Simple'>Simple</a></li>
<li type='1'><a href='#Objects-as-Params'>Objects as Params</a></li>
</ol>
</ol>
</ol>
Table of Contents
Info¶
Widgets
Use the %%interact expr0;expr1;... cell magic to set widgets for that cell.
Each expression is of the form x=y, where x becomes the local var of the cell and y can be a:
listto indicate choices for a dropdown widgettupleto indicate values for an int slider (start, stop, and optional step).
E.g.:
%%interact column=list(df.columns)
%%interact column=list(df.columns);mult=[1, 2, 3]
%%interact column=list(df.columns);window=(10, 50)
%%interact column=list(df.columns);window=(10, 50, 5)
Table of Contents
Use the %toc line magic to substitute with a table of contents in static mode.
Toggles
Click the Toggle input cells button at the bottom to reveal input cells.
Data¶
Using randomly generated data (with fixed seed).
In [6]:
df = pd.DataFrame(
np.random.RandomState(42).rand(100, 10), columns=list("abcdefghij")
)
Visualization¶
Simple¶
In [7]:
%%interact column=list(df.columns);mult=[1, 2, 3]
title = f"column={column!r}, mult={mult}"
(df[[column]] * mult).plot(figsize=(15, 8), lw=2, title=title)
Generating results: 0%| | 0/30 [00:00<?, ?it/s]
Generating results: 60%|███████████████████████████████████████████████████████████████████████████████████████████▊ | 18/30 [00:00<00:00, 173.63it/s]
Generating results: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:00<00:00, 142.78it/s]
Generating HTML: 0%| | 0/30 [00:00<?, ?it/s]
Generating HTML: 7%|██████████▌ | 2/30 [00:00<00:01, 18.47it/s]
Generating HTML: 17%|██████████████████████████▎ | 5/30 [00:00<00:01, 19.61it/s]
Generating HTML: 27%|██████████████████████████████████████████▏ | 8/30 [00:00<00:01, 19.90it/s]
Generating HTML: 37%|█████████████████████████████████████████████████████████▌ | 11/30 [00:00<00:00, 20.14it/s]
Generating HTML: 47%|█████████████████████████████████████████████████████████████████████████▎ | 14/30 [00:00<00:00, 20.20it/s]
Generating HTML: 57%|████████████████████████████████████████████████████████████████████████████████████████▉ | 17/30 [00:00<00:00, 20.31it/s]
Generating HTML: 67%|████████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 20/30 [00:00<00:00, 20.42it/s]
Generating HTML: 77%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 23/30 [00:01<00:00, 20.41it/s]
Generating HTML: 87%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 26/30 [00:01<00:00, 20.40it/s]
Generating HTML: 97%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 29/30 [00:01<00:00, 20.36it/s]
Generating HTML: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:01<00:00, 20.24it/s]
Out[7]:
mult:
In [8]:
%%interact column=list(df.columns);window=(10, 50, 5)
ax = (
df[[column]]
.iloc[-window:]
.plot(figsize=(15, 8), lw=2, title=f"column={column!r}, window={window}")
)
ax
Generating results: 0%| | 0/90 [00:00<?, ?it/s]
Generating results: 16%|███████████████████████▊ | 14/90 [00:00<00:00, 103.05it/s]
Generating results: 36%|██████████████████████████████████████████████████████▍ | 32/90 [00:00<00:00, 138.71it/s]
Generating results: 53%|█████████████████████████████████████████████████████████████████████████████████▌ | 48/90 [00:00<00:00, 127.52it/s]
Generating results: 73%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 66/90 [00:00<00:00, 142.32it/s]
Generating results: 93%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 84/90 [00:00<00:00, 151.96it/s]
Generating results: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:00<00:00, 132.55it/s]
Generating HTML: 0%| | 0/90 [00:00<?, ?it/s]
Generating HTML: 2%|███▌ | 2/90 [00:00<00:04, 19.96it/s]
Generating HTML: 6%|████████▊ | 5/90 [00:00<00:04, 21.17it/s]
Generating HTML: 9%|██████████████ | 8/90 [00:00<00:03, 21.49it/s]
Generating HTML: 12%|███████████████████▏ | 11/90 [00:00<00:03, 21.97it/s]
Generating HTML: 16%|████████████████████████▍ | 14/90 [00:00<00:03, 22.22it/s]
Generating HTML: 19%|█████████████████████████████▋ | 17/90 [00:00<00:03, 22.05it/s]
Generating HTML: 22%|██████████████████████████████████▉ | 20/90 [00:00<00:03, 22.20it/s]
Generating HTML: 26%|████████████████████████████████████████ | 23/90 [00:01<00:03, 22.23it/s]
Generating HTML: 29%|█████████████████████████████████████████████▎ | 26/90 [00:01<00:02, 22.17it/s]
Generating HTML: 32%|██████████████████████████████████████████████████▌ | 29/90 [00:01<00:02, 22.50it/s]
Generating HTML: 36%|███████████████████████████████████████████████████████▊ | 32/90 [00:01<00:02, 22.57it/s]
Generating HTML: 39%|█████████████████████████████████████████████████████████████ | 35/90 [00:01<00:02, 22.53it/s]
Generating HTML: 42%|██████████████████████████████████████████████████████████████████▎ | 38/90 [00:01<00:02, 22.28it/s]
Generating HTML: 46%|███████████████████████████████████████████████████████████████████████▌ | 41/90 [00:01<00:02, 22.41it/s]
Generating HTML: 49%|████████████████████████████████████████████████████████████████████████████▊ | 44/90 [00:01<00:02, 22.41it/s]
Generating HTML: 52%|█████████████████████████████████████████████████████████████████████████████████▉ | 47/90 [00:02<00:01, 22.63it/s]
Generating HTML: 56%|███████████████████████████████████████████████████████████████████████████████████████▏ | 50/90 [00:02<00:01, 22.65it/s]
Generating HTML: 59%|████████████████████████████████████████████████████████████████████████████████████████████▍ | 53/90 [00:02<00:01, 22.43it/s]
Generating HTML: 62%|█████████████████████████████████████████████████████████████████████████████████████████████████▋ | 56/90 [00:02<00:01, 22.63it/s]
Generating HTML: 66%|██████████████████████████████████████████████████████████████████████████████████████████████████████▉ | 59/90 [00:02<00:01, 22.70it/s]
Generating HTML: 69%|████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ | 62/90 [00:02<00:01, 22.70it/s]
Generating HTML: 72%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████▍ | 65/90 [00:02<00:01, 22.46it/s]
Generating HTML: 76%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 68/90 [00:03<00:00, 22.05it/s]
Generating HTML: 79%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 71/90 [00:03<00:00, 21.89it/s]
Generating HTML: 82%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 74/90 [00:03<00:00, 22.19it/s]
Generating HTML: 86%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 77/90 [00:03<00:00, 22.27it/s]
Generating HTML: 89%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 80/90 [00:03<00:00, 22.08it/s]
Generating HTML: 92%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 83/90 [00:03<00:00, 22.21it/s]
Generating HTML: 96%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 86/90 [00:03<00:00, 22.46it/s]
Generating HTML: 99%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 89/90 [00:03<00:00, 22.57it/s]
Generating HTML: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 90/90 [00:04<00:00, 22.32it/s]
Out[8]:
window:
Objects as Params¶
In [9]:
@dataclasses.dataclass(frozen=True)
class Calculator(abc.ABC):
@abc.abstractmethod
def calc(self, df: pd.DataFrame) -> pd.DataFrame: ...
@dataclasses.dataclass(frozen=True)
class CumSumCalculator(Calculator):
@override
def calc(self, df: pd.DataFrame) -> pd.DataFrame:
return df.cumsum()
@dataclasses.dataclass(frozen=True)
class EWMMeanCalculator(Calculator):
com: float
@override
def calc(self, df: pd.DataFrame) -> pd.DataFrame:
return df.ewm(com=self.com).mean()
calcs = [
CumSumCalculator(),
EWMMeanCalculator(com=5),
EWMMeanCalculator(com=10),
]
In [10]:
%%interact column_group=["abc", "def", "ghij"];calc=calcs
calced_df = calc.calc(df[list(column_group)])
title = f"column_group={column_group!r}, calc={calc}"
calced_df.plot(figsize=(15, 8), lw=2, title=title)
Generating results: 0%| | 0/9 [00:00<?, ?it/s]
Generating results: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 147.98it/s]
Generating HTML: 0%| | 0/9 [00:00<?, ?it/s]
Generating HTML: 33%|█████████████████████████████████████████████████████ | 3/9 [00:00<00:00, 20.81it/s]
Generating HTML: 67%|██████████████████████████████████████████████████████████████████████████████████████████████████████████ | 6/9 [00:00<00:00, 17.07it/s]
Generating HTML: 89%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 8/9 [00:00<00:00, 17.26it/s]
Generating HTML: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 17.42it/s]
Out[10]:
column_group:
In [11]:
# Auto-generated by leda
import leda
leda.show_input_toggle()
Out[11]:
In [12]:
# Auto-generated by leda
import leda
leda.show_std_output_toggle()
Out[12]: